#include "../StdInc.h"

#include <boost/format.hpp>
using namespace boost;

namespace GSTrader
{
	namespace TradeBase
	{
		COrderCondition::COrderCondition()
		{
			m_nPriceType = ocPriceNew;
			m_nConditionType = ocdCondgt;
			m_dPrice = 0;
			m_bCondition = false;
			m_time = 0;
			
		}


		COrderCondition::~COrderCondition()
		{
		}

		string COrderCondition::ToString(CInstrument* pInstrument, CParkedOrder* pOrder)
		{
			if (pInstrument == NULL || pOrder == NULL)
				return "";

			int nDecimal = pInstrument->GetDecimalLen();

			string strMsg = "";

			strMsg += (g_language == slChinese ? Chinese::c_szOcPrice[pOrder->m_nPriceType] : English::c_szOcPrice[pOrder->m_nPriceType]);
			strMsg += (g_language == slChinese ? Chinese::c_szOcCond[pOrder->m_nConditionType] : English::c_szOcCond[pOrder->m_nConditionType]);

			format fmt("%%.%df");
			fmt % nDecimal;

			format fmt2(fmt.str());
			fmt2 % pOrder->m_dPrice;

			strMsg += fmt2.str();

			format fmt3(g_language == slChinese ? Chinese::c_szFmt56 : English::c_szFmt56);
			fmt3 % strMsg;

			return fmt3.str();
		}

		bool COrderCondition::Match(CDepthMarketData* pMarketData)
		{
			CParkedOrder order;
			order.SetCondition(this);

			return Match(pMarketData, &order);
		}

		bool COrderCondition::Match(CDepthMarketData* pMarketData, CParkedOrder* pOrder)
		{
			if (!pOrder || !pOrder->m_bCondition)
				return false;


			double dPrice = 0;
			switch (pOrder->m_nPriceType)
			{
			case ocPriceNew:
				{
					dPrice = pMarketData->m_dPrice;
				}
				break;
			case ocPriceBuy1:
				{
					dPrice = pMarketData->m_dBidPrice1;
				}
				break;
			case ocPriceSell1:
				{
					dPrice = pMarketData->m_dAskPrice1;
				}
				break;
			}

			if (dPrice < DBL_MIN)
				return false;

			switch (pOrder->m_nConditionType)
			{
			case ocdCondgt:
				{
					return dPrice > pOrder->m_dPrice;
				}
				break;
			case ocdCondmoe:
				{
					return dPrice - pOrder->m_dPrice > -DBL_MIN;
				}
				break;
			case ocdCondlt:
				{
					return dPrice < pOrder->m_dPrice;
				}
				break;
			case ocdCondloe:
				{
					return pOrder->m_dPrice - dPrice > -DBL_MIN;
				}
				break;
			}

			return false;
		}
	}
}